
export default {
  namespaced: true,
  state: {
    total: 0
  },
  getters: {
    total: state => state.total,
    showTotal: state => {
      let text = ''
      if (state.total > 0) {
        text = '' + state.total
      }
      if (text > 99) {
        text = '99+'
      }
      return text
    }
  },
  mutations: {
    SET_TOTAL: (state, total) => {
      state.total = +total
    },
    SET_CHANGE_TOTAL: (state, list) => {
      if (!Array.isArray(list) || list.length === 0) {
        state.total = 0
      } else {
        let sum = 0
        list.forEach(x => {
          if (x.selected) {
            sum += +x.quantity
          }
        })
        state.total = sum
      }
    }
  },
  actions: {
    set_total: ({ commit }, total) => {
      commit('SET_TOTAL', total)
    },
    set_change_list: ({ commit }, list) => {
      commit('SET_CHANGE_TOTAL', list)
    }
  }
}
